//****************************************************
//NOTE: If you would like to enable connection manager you must have Smart Device Framework installed on your
//      development machine.  Once you have installed the SDF uncomment the define below
//****************************************************

//#define SDF_CONMGR

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.Net;


namespace RasDemo
{
    public partial class MainForm : Form
    {
        private DeviceInfo[] devices;
        private RasEntryCollection rasEntries;
#if SDF_CONMGR
        private OpenNETCF.Net.ConnectionManager connManager;
#endif
        public MainForm()
        {
            InitializeComponent();
            #if SDF_CONMGR
            //Create a new connection manager.  NOTE: this is no longer in the RAS Library but is available in the Smart Device Framework (www.smartdeviceframework.com)
            connManager = new ConnectionManager();
#endif
            //Load the entries
            LoadEntries(lstEntries);
        }

        private void menuItem2_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        private void cmdLoadEntries_Click(object sender, System.EventArgs e)
        {
            LoadEntries(lstEntries);

        }

        private void lstEntries_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            RasEntry entry = rasEntries[lstEntries.SelectedIndex];
            txtPhone.Text = entry.PhoneNumber;
            txtAreaCode.Text = entry.AreaCode;
            txtDevice.Text = entry.DeviceName;
            txtIPAddress.Text = entry.IPAddress;
            txtDns.Text = entry.IPAddressDns;
        }

        private void cmdDial_Click(object sender, System.EventArgs e)
        {
            RasEntry entry = rasEntries[lstEntries.SelectedIndex];
            entry.RasStatus += new RasNotificationHandler(entry_RasStatus);
            cmbStatus.Items.Clear();
            entry.Dial(true);
        }

        private void entry_RasStatus(int hConn, RasConnState State, RasError ErrorCode)
        {
            cmbStatus.Items.Add(State.ToString() + " " + ErrorCode.ToString());
            cmbStatus.SelectedIndex = cmbStatus.Items.Count - 1;
        }

        private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            switch (tabControl1.SelectedIndex)
            {
                case 0:
                    LoadEntries(lstEntries);
                    break;
                case 1:
                    LoadDevices();
                    break;
                case 2:
                    LoadConnections();
                    break;

            }
        }

        private void LoadConnections()
        {
            #if SDF_CONMGR
            connManager.ConnectionStateChanged += new EventHandler(connManager_ConnectionStateChanged);


            foreach (DestinationInfo dest in connManager.EnumDestinations())
            {
                lbDestinations.Items.Add(new DestinationInfoListItem(dest));
            }
            LoadEntries(lbEntries);
#endif

        }

        private class DestinationInfoListItem
        {
#if SDF_CONMGR
            DestinationInfo m_di;
            public DestinationInfoListItem(DestinationInfo di)
            {
                m_di = di;
            }
            public override string ToString()
            {
                return m_di.Description;
            }
#endif
        }

        void connManager_ConnectionStateChanged(object sender, EventArgs e)
        {
            #if SDF_CONMGR
            listBox1.Items.Add(string.Format("{0}", connManager.Status));
#endif
        }


        private void LoadEntries(ListBox lstList)
        {
            rasEntries = Ras.Entries;
            lstEntries.Items.Clear();
            for (int i = 0; i < rasEntries.Count; i++)
            {
                lstList.Items.Add(rasEntries[i].Name);
            }
            lstList.SelectedIndex = 0;

        }

        private void LoadDevices()
        {
            devices = Ras.Devices;
            cmbDevices.Items.Clear();
            for (int i = 0; i < devices.Length; i++)
            {
                cmbDevices.Items.Add(devices[i].DeviceName);
            }
            if (cmbDevices.Items.Count > 0)
                cmbDevices.SelectedIndex = 0;
        }

        private void cmdCreate_Click(object sender, System.EventArgs e)
        {

            RasEntry rasEnt = new RasEntry();
            rasEnt.Name = txtName.Text;
            if (chkOptions.Checked)
                rasEnt.Options |= (int)ConnectionOptions.SpecificIpAddr;
            else
                rasEnt.Options |= (int)ConnectionOptions.UseCountryAndAreaCodes;

            rasEnt.CountryCode = 1;
            rasEnt.AreaCode = txtNewAreaCode.Text;
            rasEnt.PhoneNumber = txtNewPhone.Text;
            rasEnt.DeviceName = devices[cmbDevices.SelectedIndex].DeviceName;
            rasEnt.DeviceType = devices[cmbDevices.SelectedIndex].DeviceType;
            rasEnt.IPAddress = txtNewIPAddress.Text;
            rasEnt.IPAddressDns = txtNewDNS.Text;

            RasDialParams dialParams = new RasDialParams();

            dialParams.UserName = txtUser.Text;
            dialParams.Password = txtPassword.Text;

            rasEnt.Options |= (int)ConnectionOptions.UseLogonCredentials;

            Ras.CreateEntry(rasEnt, dialParams);

        }

        private void button5_Click(object sender, System.EventArgs e)
        {
            #if SDF_CONMGR
            connManager.RequestDisconnect();
#endif
        }

        private void button6_Click(object sender, System.EventArgs e)
        {
            #if SDF_CONMGR
            connManager.Connect();//"http://www.microsoft.com", 0, 0, 30000);
#endif
        }

        private void button7_Click(object sender, System.EventArgs e)
        {
            #if SDF_CONMGR
            ConnectionStatus stat = connManager.Status;
            listBox1.Items.Add(string.Format("{0} = {1:X}", stat, (int)stat));
#endif
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            RasDialParams dialParams = new RasDialParams();

            //Get the entry
            RasEntry entry = rasEntries[lstEntries.SelectedIndex];
            entry.RasStatus += new RasNotificationHandler(entry_RasStatus);
            entry.Options |= (int)ConnectionOptions.DialAsLocalCall;
            entry.Options |= (int)ConnectionOptions.SpecificIpAddr;

            //Clear the status combobox
            cmbStatus.Items.Clear();
            //Dial
            entry.Dial(true, dialParams);

        }

        private void cmdHangUp_Click(object sender, System.EventArgs e)
        {
            RasEntry entry = rasEntries[lstEntries.SelectedIndex];
            entry.Hangup();

        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            RasConnection[] conn = Ras.ActiveConnections;
            if (conn != null)
                MessageBox.Show(conn[0].Name + " " + conn.Length.ToString());
        }

        private void button3_Click(object sender, System.EventArgs e)
        {
            RasEntry entry = rasEntries[lstEntries.SelectedIndex];
            Ras.DeleteEntry(entry);
            //entry.PhoneNumber = "(111)444 5555";

            //entry.

        }

    }
}